#!/bin/bash

#Created by Turk 2017-07-31
#Version 1.0
#NYX STARTUP

if [ $# -lt 1 ]; then
	echo `date +"%Y-%m-%d %H:%M:%S"`  "Usage: START|STOP|RESTART"
	exit 1
fi

COMMAND=$1

TOMCATPATH=/home/hadoop/tomcat6
FLINKPATH=/usr/local/flink


function startup()
{
	#START MYSQL
	echo 'MYSQL START...'
	NUM=`netstat -lanp|grep tcp|grep 3306|wc -l`
	if [ $NUM -gt 0 ]; then
		echo 'MYSQL is Running!'
	else
		service mysql start
		echo 'MYSQL START SUCCESS!'
	fi
	
	
	#START TOMCAT
	echo 'TOMCAT START...'
	$TOMCATPATH/bin/shutdown.sh
	$TOMCATPATH/bin/startup.sh
	echo 'TOMCAT START SUCCESS!'
	
	#START MONGODB
	echo 'MONGODB START...'
	service mongod restart
	echo 'MONGODB START SUCCESS!'
	
	#START FLINK
	echo 'FLINK START...'
	$FLINKPATH/bin/stop-local.sh
	$FLINKPATH/bin/start-local.sh
	echo 'FLINK START SUCCESS!'
	
	#START FLINK Program
	echo 'FLINK START...'
	
	echo 'FLINK START SUCCESS!'
}

function stop()
{
	#START MYSQL
	echo 'MYSQL STOP...'
	service mysql stop
	echo 'MYSQL STOP Complate'
	
	
	#START TOMCAT
	echo 'TOMCAT STOP...'
	$TOMCATPATH/bin/shutdown.sh
	echo 'TOMCAT STOP Complate'
	
	#START MONGODB
	echo 'MONGODB STOP...'
	service mongod stop
	echo 'MONGODB STOP Complate'
	
	#START FLINK
	echo 'FLINK STOP...'
	$FLINKPATH/bin/stop-local.sh
	echo 'FLINK STOP Complate'
	
	#START FLINK Program
	echo 'FLINK STOP...'
	
	echo 'FLINK STOP SUCCESS!'
}

if [ $COMMAND = "START" ]; then
	startup;
elif [ $COMMAND = "STOP" ]; then
	stop;
fi

done
